import hashlib


def md5hex(word):
    if isinstance(word, unicode):
        word = word.encode("utf-8")
    elif not isinstance(word, str):
        word = str(word)
    m = hashlib.md5()
    m.update(word)
    return m.hexdigest()


# 参数名
paramas_name = [
    'android_id',''
    'channel',
    'deviceId',
    'device_fingerprint',
    'imei',
    'lang',
    'password',
    'phone',
    'platform',
    'sid',
    'start',
    't',
    'type',
    'versionName',
    'zone'
]


# 按参数名顺序传入参数值列表，无参数名留空值
def get_sign(paramas_value):
    key = ''
    for index, item in enumerate(paramas_value):
        if item != '':
            key = key + paramas_name[index] + '%3D' + item
    deviceId = paramas_value[2]
    v1_2 = bytearray(key, 'utf-8')
    v5_1 = ''
    v3_2 = 0
    v2 = 0
    v4_1 = bytearray(deviceId, 'utf-8')

    while v2 < len(v1_2):
        v5_1 = v5_1 + str(v1_2[v2] ^ v4_1[v3_2])
        v3_2 = (v3_2 + 1) % len(v4_1)
        v2 = v2 + 1

    sign = md5hex(md5hex(v5_1) + deviceId)
    return sign

get_sign()